<!--私有库存-->
<script setup>
import { ref, reactive, toRefs, computed, onMounted } from 'vue';
import { useI18n } from 'vue-i18n';
const { t } = useI18n();
import {
  getMailingList,
  addPcbaOrderToExpress,
  queryPagePcbaSmtBomOrderExpressList,
  getBomOrderDetail,
} from '@/server/api/biz.js';
import Pagination from '@/components/Pagination/index.vue';
import Table from '@/components/Table/index.vue';
import MailingInfoModel from './MailingInfoModel.vue';

const centerDialogVisible = ref(false);
const elementInfoVisible = ref(false);
const columns = computed(() => [
  {
    label: t('om.OrderNo'),
    prop: 'serial',
  },
  {
    label: t('om.OrderInfo'),
    prop: 'item',
  },
  {
    label: t('mall.shipping_information'),
    prop: 'createBy',
  },
  {
    label: t('om.Operate'),
    type: 'btn',
  },
]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderNo: undefined,
  },
});

const mailingData = reactive({
  list: {},
  serialNumber: '',
  id: '',
  bomData: {},
  serial: '',
  bomId: '',
});
const mailingInfoModel = ref(null);
const { queryParams } = toRefs(data);

const emit = defineEmits(['handleTabs', 'updateGoMailData']);

/**
 * 获取待邮寄列表
 */
const getList = (obj) => {
  getMailingList(obj).then((res) => {
    console.log(res);
    if (res.code === 200) {
      mailingData.list = res.data;
    } else {
      ElMessage.error(res.msg);
    }
  });
};

onMounted(() => {
  init();
});

const init = () => {
  const obj = {
    bomSource: 2,
    limit: 10,
    page: 1,
    pageType: 1,
  };
  getList(obj);
};

/**
 * 加入邮寄清单
 * @param {*} row
 */
const addPackage = (row) => {
  console.log(row);
  mailingData.serialNumber = row.serial;
  mailingData.id = row.id;
  centerDialogVisible.value = true;
};

/**
 * 确认加入包裹
 */
const confirmAddPackage = () => {
  addPcbaOrderToExpress(mailingData.id).then((res) => {
    if (res.code === 200) {
      ElMessage.success(t('om.Addparcelsuccessfully'));
      centerDialogVisible.value = false;
      init();
      emit('handleTabs', 2);
      // emit('updateGoMailData');
    } else {
      ElMessage.error(res.msg);
      centerDialogVisible.value = false;
    }
  });
};
/**
 * 分页
 * @param {*} page
 * @param {*} limit
 */
const handlePaginationChange = (page = 1, limit = 10) => {
  const obj = {
    bomSource: 2,
    bomStatus: 0,
    contractId: 0,
    customerAccountId: 0,
    customerId: 0,
    limit: limit,
    orderCategory: 0,
    orderLevel: 0,
    orderProgress: 0,
    orderQuoteType: 0,
    orderSource: 0,
    orderStatus: 0,
    orderType: 0,
    page: page,
    pageType: 1,
    patchStatus: 0,
    pcbStatus: 0,
    serial: '',
    smtLayer: 0,
  };
  getList(obj);
};

/**
 * 查看邮寄信息
 */
const elementInfoOPen = (row) => {
  mailingData.serial = row.serial;
  mailingData.bomId = row.id;
  elementInfoVisible.value = true;
  mailingInfoModel.value.getBomOrderDetailList(1, 10, row.id);
  mailingInfoModel.value.getQueryPcbaPcbOrderList(1, 10, row.id);
};
</script>
<template>
  <div class="table-wrap">
    <Table
      :data="mailingData.list.list"
      :columns="columns"
      serial
      :serialOptions="{ pageNum: mailingData.list.pageNumber, pageSize: mailingData.list.pageSize }"
      @elementInfoOPen="elementInfoOPen"
    >
      <template #btnSlot="{ scope }">
        <el-button size="small" type="warning" @click="addPackage(scope.row)">{{
          $t('om.AddPackage')
        }}</el-button>
      </template>
    </Table>
    <pagination
      v-show="mailingData.list.totalRow > 0"
      :total="mailingData.list.totalRow"
      v-model:page="mailingData.list.pageNumber"
      v-model:limit="mailingData.list.pageSize"
      @pagination="handlePaginationChange"
    />
  </div>
  <el-dialog v-model="centerDialogVisible" :title="$t('om.AddPackage')" width="25%" center>
    <span>{{ $t('om.confirm_add_all_components_to_cart', { name: mailingData.serialNumber }) }}</span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="centerDialogVisible = false">{{ $t('smt.Cancel') }}</el-button>
        <el-button type="primary" @click="confirmAddPackage"> {{ $t('smt.Define') }} </el-button>
      </span>
    </template>
  </el-dialog>
  <MailingInfoModel
    v-model="elementInfoVisible"
    type="2"
    :serial="mailingData.serial"
    :bomId="mailingData.bomId"
    ref="mailingInfoModel"
  />
</template>
<style lang="scss" scoped>
.table-wrap {
  padding-top: 1px;
}
</style>
